﻿using IniReadWrite;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionRoc.Tool;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;

namespace VisionRoc.Core
{
    public class ImageAutoDelect
    {
        string m_Path;
        public ImageAutoDelect(string _Path)
        {
            //判断文件路径是否存在
            if (!Directory.Exists(_Path))
            {
                Directory.CreateDirectory(_Path);
            }
            m_Path = _Path + "\\ImageAutoDelect.ini";
            if (File.Exists(m_Path))
            {
                ReadParameter();
            }
        }

        /// <summary>
        /// 结果图像路径
        /// </summary>
        public string ResultImagePath;

        /// <summary>
        /// 原图图像路径
        /// </summary>
        public string ImagePath;

        /// <summary>
        /// 结果图像保存天数
        /// </summary>
        public int ResultSaveDay;

        /// <summary>
        /// 原图图像保存天数
        /// </summary>
        public int ImageSaveDay;

        /// <summary>
        /// 执行时间
        /// </summary>
        public double time;

        public async void ResultImageFileMonitor(Panel ResultImageState)
        {
            await Task.Run(() =>
            {
                if (ResultSaveDay == 0)
                {
                    Global.AddAutoImageDelectEvent(LogLevel.Warn, "【结果图保存天数为0监控失败！！！】");
                    //ResultImageSate = 2;
                    ResultImageState.BackColor = Color.Red;
                    return;
                }
                if (!Directory.Exists(ResultImagePath))
                {
                    Global.AddAutoImageDelectEvent(LogLevel.Warn, ResultImagePath + "  【结果图图像路径不存在监控失败】");
                    //ResultImageSate = 2;
                    ResultImageState.BackColor = Color.Red;
                    return;
                }

                if (true)
                {
                    try
                    {
                        DirectoryInfo d = new DirectoryInfo(ResultImagePath);
                        FileSystemInfo[] fsinfos = d.GetFileSystemInfos();
                        foreach (FileSystemInfo item in fsinfos)
                        {
                            if (item is DirectoryInfo)
                            {
                                if (Directory.Exists(item.FullName))
                                {
                                    //如果文件存在获取文件创建日期
                                    TimeSpan t = DateTime.Now - Directory.GetCreationTime(item.FullName);
                                    int day = t.Days;
                                    //如果文件创建创建日期大于我的设定日期则删除文件
                                    if (ResultSaveDay < day)
                                    {
                                        DirectoryInfo di = new DirectoryInfo(item.FullName);
                                        Global.AddAutoImageDelectEvent(LogLevel.Info, "【正在删除超过规定时间内路径下的结果图像文件】");
                                        Directory.Delete(item.FullName, true);
                                        Global.AddAutoImageDelectEvent(LogLevel.Info, item.Name + "【结果图像文件删除成功】");
                                    }
                                }
                                else
                                {
                                    Global.AddAutoImageDelectEvent(LogLevel.Warn, item.Name + "【结果图像文件不在磁盘中,自动删除线程退出。】");
                                    //ResultImageSate = 2;
                                    ResultImageState.BackColor = Color.Red;
                                    return;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        //ResultImageSate = 2;
                        Global.AddAutoImageDelectEvent(LogLevel.Warn, "【自动删除结果图像中发生异常！！！】");
                        TxtAPI.WriteLogTxt("自动删除结果图像线程发生异常:" + ex.ToString());
                        ResultImageState.BackColor = Color.Red;
                        return;
                    }
                }


            });
        }

        public async void ImageFileMonitor(Panel ImageSate)
        {
            await Task.Run(() =>
            {
                if (ImageSaveDay == 0)
                {
                    Global.AddAutoImageDelectEvent(LogLevel.Warn, "【原图保存天数为0监控失败！！！】");
                    //ImageState = 2;
                    ImageSate.BackColor = Color.Red;
                    return;
                }
                if (!Directory.Exists(ImagePath))
                {
                    Global.AddAutoImageDelectEvent(LogLevel.Warn, ImagePath + "  【原图图像路径不存在监控失败！！！】");
                    //ImageState = 2;
                    ImageSate.BackColor = Color.Red;
                    return;
                }
                //ImageState = 1;
                //Global.AutoImageInfo(LogLevel.OK, "【原图图像文件自动删除监控中···】");

                if (true)
                {
                    try
                    {
                        //panel.BackColor = Color.Green;
                        DirectoryInfo d = new DirectoryInfo(ImagePath);
                        FileSystemInfo[] fsinfos = d.GetFileSystemInfos();
                        foreach (FileSystemInfo item in fsinfos)
                        {
                            if (item is DirectoryInfo)
                            {
                                if (Directory.Exists(item.FullName))
                                {
                                    //如果文件存在获取文件创建日期
                                    TimeSpan t = DateTime.Now - Directory.GetCreationTime(item.FullName);
                                    int day = t.Days;
                                    //如果文件创建创建日期大于我的设定日期则删除文件
                                    if (ResultSaveDay < day)
                                    {
                                        DirectoryInfo di = new DirectoryInfo(item.FullName);
                                        Global.AddAutoImageDelectEvent(LogLevel.Info, "【正在删除超过规定时间内路径下的原图文件】");
                                        Directory.Delete(item.FullName, true);
                                        Global.AddAutoImageDelectEvent(LogLevel.Info, item.Name + "【原图文件删除成功】");
                                    }
                                }
                                else
                                {
                                    Global.AddAutoImageDelectEvent(LogLevel.Warn, item.Name + "【原图图像文件不在磁盘中,自动删除线程退出。】");
                                    //ImageState = 2;
                                    return;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        //ImageState = 2;
                        Global.AddAutoImageDelectEvent(LogLevel.Warn, "【自动删除原图图像中发生异常！！！】");
                        TxtAPI.WriteLogTxt("自动删除原图图像中发生异常:" + ex.ToString());
                        return;
                    }
                }

            });
        }

        /// <summary>
        /// 图像自动删除读取参数
        /// </summary>
        public void ReadParameter()
        {
            try
            {
                ResultImagePath = IniAPI.INIGetStringValue(m_Path, "ImageAutoDelectInfo", "ResultImagePath", "0");
                ImagePath = IniAPI.INIGetStringValue(m_Path, "ImageAutoDelectInfo", "ImagePath", "0");
                ResultSaveDay = Convert.ToInt32(IniAPI.INIGetStringValue(m_Path, "ImageAutoDelectInfo", "ResultSaveDay", "0"));
                ImageSaveDay = Convert.ToInt32(IniAPI.INIGetStringValue(m_Path, "ImageAutoDelectInfo", "ImageSaveDay", "0"));
                time = Convert.ToDouble(IniAPI.INIGetStringValue(m_Path, "ImageAutoDelectInfo", "time", "1"));
            }
            catch
            {

            }
        }

        /// <summary>
        /// 保存图像自动删除状态
        /// </summary>
        public void WriteParameter()
        {
            IniAPI.INIWriteValue(m_Path, "ImageAutoDelectInfo", "ResultImagePath", ResultImagePath.ToString());
            IniAPI.INIWriteValue(m_Path, "ImageAutoDelectInfo", "ImagePath", ImagePath.ToString());
            IniAPI.INIWriteValue(m_Path, "ImageAutoDelectInfo", "ResultSaveDay", ResultSaveDay.ToString());
            IniAPI.INIWriteValue(m_Path, "ImageAutoDelectInfo", "ImageSaveDay", ImageSaveDay.ToString());
            IniAPI.INIWriteValue(m_Path, "ImageAutoDelectInfo", "time", time.ToString());
        }

    }
}
